package com.example.dryclean_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.Detergent;
import com.example.dryclean_admin.entity.Orderlist;
import com.example.dryclean_admin.entity.Orderlog;
import com.example.dryclean_admin.service.IDetergentService;
import com.example.dryclean_admin.service.IOrderlistService;
import com.example.dryclean_admin.service.IOrderlogService;
import com.example.dryclean_admin.service.IProgressStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiaxin
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/orderlist")
public class OrderlistController {
    @Autowired
    public IOrderlistService iOrderlistService;
    @Autowired
    private IOrderlogService orderlogService;
    @Autowired
    private IProgressStageService progressStageService;
    @Autowired
    private IDetergentService detergentService;

    @RequestMapping("/list")
    public Result list(){
        return Result.success(iOrderlistService.list());
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Orderlist orderlist){
        return iOrderlistService.save(orderlist)?Result.success():Result.fail();
    }

    public OrderlistController(IOrderlistService iOrderlistService) {
        this.iOrderlistService = iOrderlistService;
    }

    /**
     * 查询订单进度
     * @param orderId 订单 ID
     * @return 订单进度信息
     */
    @GetMapping("/progress")
    public Result getOrderProgress(@RequestParam Integer orderId) {
        Map<String, Object> progress = iOrderlistService.getOrderProgress(orderId);
        if (progress != null) {
            return Result.success(progress);
        }
        return Result.fail();
    }

    @PostMapping("/updateDetergent")
    public Result updateDetergent(@RequestBody Orderlist orderlist){
        Orderlist byId = iOrderlistService.getById(orderlist.getId());
        byId.setDetergentId(orderlist.getDetergentId());
        return iOrderlistService.updateById(byId)?Result.success():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Orderlist orderlist){
        return iOrderlistService.updateById(orderlist)?Result.success():Result.fail();
    }

    /*public Result login(@RequestParam("account") String account, @RequestParam("password") String password){
        //1.非空校验
        if(!StringUtils.hasLength(account) || !StringUtils.hasLength((password))){
            return Result.fail();
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount,account)
                .eq(User::getPassword,password);
        User user = userService.getOne(wrapper);
        if(user != null){
            List menuList = iMenuService.lambdaQuery().like(Menu::getMenuRight,user.getRoleid()).list();
            HashMap res = new HashMap();
            res.put("user",user);
            res.put("menu",menuList);
            return Result.success(res);
        }
        return Result.fail();
    }*/

    @RequestMapping("/updatestate")
    public Result update(@RequestParam String id, @RequestParam String state){
        LambdaQueryWrapper<Orderlist> wrapper = new LambdaQueryWrapper<Orderlist>();
        if(StringUtils.hasLength(id) && !"null".equals(id)){

            wrapper.eq(Orderlist::getId,id);
            Orderlist orderlist = iOrderlistService.getOne(wrapper);
            orderlist.setState(state);
            iOrderlistService.updateById(orderlist);
            return Result.success();
        }
        return Result.fail();

        /*int id = Integer.parseInt(map.get("id"));
        String state = map.get("state");


        UpdateWrapper<Orderlist> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("state",state);


        return Result.success();*/

    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return iOrderlistService.removeById(id) ? Result.success() : Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        Page<Orderlist> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Orderlist> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(Orderlist::getUsername,username);
        }
        IPage<Orderlist> result = iOrderlistService.page(page,lambdaQueryWrapper);
        for (Orderlist orderlist : result.getRecords()) {
            Integer id = orderlist.getId();
            // 根据id查询进度列表
            List<Orderlog> orderlogs = orderlogService.listByOrderId(id);
            orderlist.setOrderlogs(orderlogs);

        }

        return Result.success(result.getRecords(), result.getTotal());
    }

    @RequestMapping("/userlist")
    public Result userlistPage(@RequestParam String username){
        List<Orderlist> list = iOrderlistService.lambdaQuery().eq(Orderlist::getUsername,username).list();
        for (Orderlist orderlist : list) {
            Integer id = orderlist.getId();
            if(orderlist.getDetergentId()!=null) {
                Detergent detergentServiceById = detergentService.getById(orderlist.getDetergentId());
                if(detergentServiceById!=null) {
                    orderlist.setDetergentName(detergentServiceById.getName());
                }
            }
            // 根据id查询进度列表
            List<Orderlog> orderlogs = orderlogService.listByOrderId(id);
            if(orderlogs != null) {
                for (Orderlog orderlog : orderlogs) {
                    orderlog.setProcessname(progressStageService.getById(orderlog.getProcessid()).getStageName());
                }
                orderlist.setOrderlogs(orderlogs);
            }
        }
        return Result.success(list);
    }

    @PostMapping("/updateProgress")
    public Result updateProgress(@RequestBody Map<String, Object> progressInfo) {
        // 这里需要实现更新订单进度和溶剂选择的逻辑
        Integer orderId = (Integer) progressInfo.get("orderId");
        Integer stageId = (Integer) progressInfo.get("stageId");
        Integer detergentId = (Integer) progressInfo.get("detergentId");

        // 更新订单信息
        Orderlist orderlist = iOrderlistService.getById(orderId);
        if (orderlist != null) {
            orderlist.setStageId(stageId);
            orderlist.setDetergentId(detergentId);
            boolean success = iOrderlistService.updateById(orderlist);
            if (success) {
                return Result.success();
            }
        }
        return Result.fail();
    }


}
